package com.example.multithread.no1114;

import java.util.concurrent.CountDownLatch;

public class VolatileFoo {

    public VolatileFoo() {

    }

    volatile int flag = 0;

    public static void main(String[] args) {
        VolatileFoo foo = new VolatileFoo();

        new Thread(() -> {
            foo.first();
        }).start();
        new Thread(() -> {
            try {
                foo.second();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                foo.third();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void first() {

        System.out.print("first");
        flag++;
    }

    public void second() throws InterruptedException {
        while (flag != 1){}
        System.out.print("two");
        flag++;
    }

    public void third() throws InterruptedException {
        while (flag != 2){}
        System.out.print("three");
    }
}
